#!/bin/sh
#desc: Crea un initrd de OpenDomo a partir del de Busybox

# Copyright(c) 2011 OpenDomo Services SL. Licensed under GPL v3 or later

# ----------------------------------------------------------------------------
#  Script Configuration
# ----------------------------------------------------------------------------
#RASPBERRY
#ROOTFS="../../buildroot/output/images/rootfs.ext2"
ROOTFS="../../buildroot/output/images/rootfs.tar.gz"

WORK=/tmp
MNTORIG=/mnt/buildroot
MNTDIR=/mnt/opendomo
#INITRDSIZE=42
INITRDSIZE=64

if test `whoami` != "root"
then
	echo "You must be root to run this script"
	exit 1
fi

if ! test -d $MNTDIR; then
	echo "WARNING: $MNTDIR does not exist. Creating..."
	mkdir $MNTDIR || (echo ERROR  && exit 1)
fi 

if ! test -d $MNTORIG; then
	echo "WARNING: $MNTORIG does not exist. Creating..."
	mkdir $MNTORIG || (echo ERROR  && exit 1)
fi 


if ! test -f "$ROOTFS"; then
	echo "ERROR: Root filesystem not found"
	echo "Execute command brmake to solve the problem"
	exit 1
fi

rm ../opendomo-pkg/pkg/*.gz
cd ../opendomo-pkg/
./mkpkg.sh > /dev/null
cd ../opendomo-distro/
PKGMANAGER=`ls ../opendomo-pkg/pkg/odpkg*.tar.gz | head -n 1` 2>/dev/null
if ! test -f "$PKGMANAGER"; then
	echo "ERROR: odpkg not found"
	pwd
	ls ../opendomo-pkg/pkg/
	exit 1
fi

if ! test -d bootcd; then
	mkdir bootcd
fi


### =========== MOUNTING FS!! ==================================================
echo -n "Mounting buildroot's inird...           "
#RASPBERRY
#mount $ROOTFS $MNTORIG -o loop || exit 1
tar -xvzf $ROOTFS -C $MNTDIR || exit 1
echo "OK"

#RASPBERRY
#echo -n "Creating empty ramdrive...              "
#dd if=/dev/zero of=bootcd/initrd count=$INITRDSIZE bs=1048576 >/dev/null 2>/dev/null
#mkfs.ext2 -F bootcd/initrd  > /dev/null 2>/dev/null
#echo "OK"

#RASPBERRY
#echo -n "Mounting new initrd...                  "
#mount bootcd/initrd $MNTDIR -o loop || (echo ERROR && exit 1) 
#echo "OK"

#RASPBERRY
#echo -n "Copying contents to the new initrd...   "
#cp -pR $MNTORIG/* $MNTDIR/ 2>/dev/null 
#echo "OK"

#RASPBERRY
#echo -n "Unmounting buildroot's initrd...        "
#umount $MNTORIG || (echo ERROR && exit 1) 
#echo "OK"

# ----------------------------------------------------------------------------
#  OpenDomo Configuration
# ----------------------------------------------------------------------------

# Remove the files that can collide if it's a re-used initrd
rm -fr $MNTDIR/var/pkg/
rm -fr $MNTDIR/etc/init.d/*
rm -fr $MNTDIR/etc/inetd.d/*
rm -fr $MNTDIR/etc/opendomo/root/*
rm -fr $MNTDIR/root

mkdir -p $MNTDIR/usr/local/opendomo/
mkdir -p $MNTDIR/etc/opendomo/
mkdir -p $MNTDIR/var/opendomo/
mkdir -p $MNTDIR/var/pkg
mkdir -p $MNTDIR/var/pkg/installed


# -Forced installation packages -
#echo "00000000" > $MNTDIR/var/pkg/installed/keymaps
#echo "00000000" > $MNTDIR/var/pkg/installed/odcgi
#echo "00000000" > $MNTDIR/var/pkg/installed/odcommon
#echo "00000000" > $MNTDIR/var/pkg/installed/oddiscovery

# Some dummy files for meta-packages
echo "0" > $MNTDIR/var/opendomo/dbg.txt
echo "0" > $MNTDIR/var/opendomo/kernel.txt

# We won't pack the drivers when making the release. Only the selected files 
# will remain in the directory and therefore will be copied
HERE=`pwd`
# Using "current" for the kernel version we can reuse the file lists

#rm  -fr $MNTDIR/lib/modules/3.1.10
ln -s `basename $MNTDIR/lib/modules/3.*` $MNTDIR/lib/modules/current 
cd $MNTDIR/
find . -type f > $HERE/drivers/files_before.all

# Removing some unnecessary files
rm -fr usr/share/sounds/alsa


# -- Drivers compiled with the kernel but excluded from the distro --
echo -n "Packing drivers:"
VERSION=`date '+%Y%m%d'`
EXCLUDE=' --exclude "*~" --exclude .svn --same-permissions'

rm $HERE/drivers/*.gz 2>/dev/null
for lst in `ls $HERE/drivers/*.lst`; do
	# this pkgname should be the one that odkoloader needs to know
	pkgname="`basename $lst | sed -e 's/.lst//'`"
	rm -fr $HERE/drivers/${pkgname}-$VERSION.i486.tar.gz
	if tar cf $HERE/drivers/${pkgname}-$VERSION.i486.tar --remove-files \
		-C $MNTDIR `cat $lst` 2> $HERE/drivers/${pkgname}.notfound
	then
		if test -d $HERE/drivers/$pkgname 
		then
			chmod 755 -R $HERE/drivers/$pkgname
			cd $HERE/drivers/$pkgname
			tar rf ../${pkgname}-$VERSION.i486.tar * $EXCLUDE
			cd ../..
		fi
		touch $HERE/drivers/${pkgname}.deps
		echo -n " $pkgname "
	else
		rm $HERE/drivers/${pkgname}-$VERSION.i486.tar 2>/dev/null
	fi
done
gzip $HERE/drivers/*.tar
chmod ugo+rw $HERE/drivers/*
# Driver packages are still included manually 
#cp drivers/* packages/
echo
cd $MNTDIR
find . -type f > $HERE/drivers/files.all
cd $HERE

# -- INETD --
mkdir -p $MNTDIR/etc/inetd.d/
mkdir -p $MNTDIR/var/www/cgi-bin
cp -R var $MNTDIR/

chown 1000 $MNTDIR/var/www/cgi-bin/od.cgi
chmod 0770 $MNTDIR/var/www/cgi-bin/od.cgi
chown 1000 $MNTDIR/var/www/*


# -- PASSWD/SHADOW --
cat $MNTDIR/etc/passwd | grep -v root | grep -v default > $WORK/.passwd
cat $MNTDIR/etc/shadow | grep -v root | grep -v default > $WORK/.shadow
cat $MNTDIR/etc/group | grep -v default > $WORK/.group
echo 'root:x:0:0:root:/home/root:/bin/sh
nobody:x:65534:65534:nobody:/dev/null:/bin/false
admin:x:1000:1000:Administrator:/home/admin:/bin/sh
user:x:1001:100:Plain user:/home/user:/bin/sh
' >> $WORK/.passwd 
echo 'root:$1$OKVezSiw$y5e5t0hpI1FZIHhStB0qd0:13345:0:99999:7:::
nobody:!:13425:0:99999:7:::
admin:$1$OKVezSiw$y5e5t0hpI1FZIHhStB0qd0:13742:0:99999:7:::
user:$1$OKVezSiw$y5e5t0hpI1FZIHhStB0qd0:17888:0:99999:7:::
' >> $WORK/.shadow
mv $WORK/.passwd $MNTDIR/etc/passwd
mv $WORK/.shadow $MNTDIR/etc/shadow
mv $WORK/.group $MNTDIR/etc/group
echo "audio:x:20:root" >> $MNTDIR/etc/group
echo "admin:x:1000:admin" >> $MNTDIR/etc/group
echo "users:x:100:user,admin" >> $MNTDIR/etc/group
echo "guests:x:200:user,admin" >> $MNTDIR/etc/group
#RASPBERRY
pwd
cp ./sudoers $MNTDIR/etc/
echo "sudo:x:27:admin" >> $MNTDIR/etc/group
chmod 4755 $MNTDIR/bin/busybox


# -- Enviroment --
rm -fr $MNTDIR/home
mkdir $MNTDIR/home
chmod 755 $MNTDIR/home
mkdir -p $MNTDIR/home/root/
mkdir -p $MNTDIR/home/admin/
chown 1000 $MNTDIR/home/admin
chmod 700 $MNTDIR/home/admin
mkdir -p $MNTDIR/home/user
chown 1001 $MNTDIR/home/user
chmod 700 $MNTDIR/home/user
#echo ". /etc/profile" > $MNTDIR/home/root/.profile
#chmod +x $MNTDIR/home/root/.profile

# ----------------------------------------------------------------------------
# 	System Initialitzation
# ----------------------------------------------------------------------------

# -- Package Repositories --
#ODVER="0.9.4"
#echo $ODVER > $MNTDIR/etc/VERSION

#touch $MNTDIR/etc/opendomo/repositories.cfg
#chown 1000 $MNTDIR/etc/opendomo/repositories.cfg
#echo "http://es.opendomo.org/files/pkg/" > $MNTDIR/etc/opendomo/repositories.cfg
#echo "http://es.opendomo.org/files/pkg/$ODVER/" >> $MNTDIR/etc/opendomo/repositories.cfg

# -- etc, var and usr folder to initrd --
rm -fr $MNTDIR/etc/init.d/*
cp -R etc $MNTDIR/
rm -fr $MNTDIR/etc/init.d/*~
cp -R var $MNTDIR/
cp -R usr $MNTDIR/
cp -R sbin $MNTDIR/
rm -fr $MNTDIR/usr/local/opendomo/*~
find $MNTDIR -name ".svn" | xargs -n 1 rm -fr
cp TODO CHANGES $MNTDIR/etc/

# -- CRON --
mkdir -p $MNTDIR/var/spool/cron/crontabs
mkdir -p $MNTDIR/etc/crond.d/

# -- Networking --
#cp etc/udhcp.script $MNTDIR/usr/share/udhcpc/default.script
#chmod 555 $MNTDIR/usr/share/udhcpc/default.script
rm -fr $MNTDIR/usr/share/udhcpc

rm $MNTDIR/usr/lib/libvolume_id.* 2>/dev/null

# -- Execution privileges --
chmod +x $MNTDIR/etc/init.d/*


# -- Supported architectures (for packages selection) --
echo 'x86|i386|386|i486|486' > $MNTDIR/etc/archs
# Every ISO is prepared for a determined architecture (in this case, 486), so
# this file will tell the package manager which packages can use apart of noarch

# -- Support scripts --
echo '#!/bin/sh
grep $USER /etc/group | cut -f1 -d:' > $MNTDIR/bin/groups
chmod 555 $MNTDIR/bin/groups

echo "" > $MNTDIR/etc/fstab

# -- File/Directory privileges -----------------
chmod u+w $MNTDIR/var/www/

chmod -R 775 $MNTDIR/var/pkg
rm -fr $MNTDIR/var/log
mkdir -p $MNTDIR/var/log
chmod 770 $MNTDIR/var/log
chgrp 1000 $MNTDIR/var/log
rm -fr $MNTDIR/var/run
mkdir -p $MNTDIR/var/run
chmod 777 $MNTDIR/var/run
chgrp 1000 $MNTDIR/var/run

chown 1000 $MNTDIR/var/pkg/
chown 1000 $MNTDIR/var/pkg/installed

chmod 444 $MNTDIR/etc/archs
touch $MNTDIR/etc/sysrep.cfg

chown 1000 $MNTDIR/usr/bin
chown 1000 $MNTDIR/usr/lib

chmod g+w $MNTDIR/etc/init.d
chgrp 1000 $MNTDIR/etc/init.d

chmod g+w $MNTDIR/etc/inetd.d
chgrp 1000 $MNTDIR/etc/inetd.d

chmod g+w $MNTDIR/etc/crond.d
chgrp 1000 $MNTDIR/etc/crond.d

chmod g+w $MNTDIR/etc/udev/rules.d
chgrp 1000 $MNTDIR/etc/udev/rules.d

mkdir -p $MNTDIR/etc/opendomo/i18n
ln -s i18n/lang $MNTDIR/etc/opendomo/lang
echo "es" > $MNTDIR/etc/opendomo/i18n/lang
chmod g+w $MNTDIR/etc/opendomo/i18n/lang
chown 1000:1000 $MNTDIR/etc/opendomo/i18n/lang

chmod g+w $MNTDIR/etc/opendomo/
echo > $MNTDIR/etc/opendomo/pkglist.txt

chown 0:1000 $MNTDIR/sbin/pkg_*

#chmod -R 770 $MNTDIR/etc/opendomo
chmod    777 $MNTDIR/tmp
chmod     +t $MNTDIR/tmp

chown root:root $MNTDIR/etc/init.d/*
chmod 500 $MNTDIR/etc/init.d/*
chgrp 1000 $MNTDIR/etc/init.d/rcS
chmod 0750 $MNTDIR/etc/init.d/rcS
#chown 1000 $MNTDIR/etc/init.d/sound

mkdir $MNTDIR/etc/opendomo/zones
chown 1000:1000 $MNTDIR/etc/opendomo/zones

chown 1000 $MNTDIR/usr/local/opendomo/*.sh
chgrp 1000 $MNTDIR/usr/local/opendomo/*.sh
chmod 750 $MNTDIR/usr/local/opendomo/*.sh

#chmod +x $MNTDIR/usr/local/bin/track_events.sh

#mkdir -p $MNTDIR/usr/share/templates/
#chown 1000:1000 $MNTDIR/usr/share/templates

mkdir -p $MNTDIR/usr/local/opendomo/services
chmod 0775 -R $MNTDIR/usr/local/opendomo/services
chown 1000:1000 $MNTDIR/usr/local/opendomo/services -R

mkdir -p $MNTDIR/etc/opendomo/states
chmod 0775 -R $MNTDIR/etc/opendomo/states
chown 1000:1000 -R $MNTDIR/etc/opendomo/states

chown 1000:1000 $MNTDIR/usr/share

chown 1000:1000 $MNTDIR/etc/opendomo/root/config/*
chmod o-wx -R $MNTDIR/etc/opendomo/root/config/*

# If odevents is not installed, use logEvent by default
ln -s /usr/local/opendomo/services/syscript/logEvent.sh $MNTDIR/bin/logevent

if test -f $MNTDIR/usr/bin/sudo; then
	chmod 4755 $MNTDIR/usr/bin/sudo
fi

#RASPBERRY
#umount $MNTDIR

#RASPBERRY
#rm -fr bootcd/initrd.gz 
#gzip bootcd/initrd
#chmod ugo+rw bootcd/* -R

